<?php

class BagController extends BaseController
{
    protected $tableName = "bag";
    protected $pk = "bag_id";

    // GET
    public function bagsByManufacturer($mnf_name): array
    {
        $mnf_name = '"' . $mnf_name . '"';
        $sql = "CALL bags_by_manufacturer($mnf_name);";
        $result = $this->conn->query($sql);
        $data = $this->getArrayDataFromResult($result);
        if (empty($data)) {
            return ReturnHelper::failed([], "mnf_name错误");
        }
        else {
            return ReturnHelper::success($data, "数据请求成功");
        }
    }

    // 重写get方法
    public function get($id): array
    {
        $sql = "SELECT * FROM bag b INNER JOIN  manufacturer m ON b.mnf_id = m.mnf_id WHERE b.bag_id = $id";
        $result = $this->conn->query($sql);
        $data = $this->getSingleDataFromResult($result);
        if (empty($data)) {
            return ReturnHelper::failed([], "请求bag信息失败");
        }
        else {
            return ReturnHelper::success($data, "数据请求成功");
        }
    }

    // 重写getList方法
    public function getList(): array
    {
        $sql = "SELECT * FROM bag b INNER JOIN  manufacturer m ON b.mnf_id = m.mnf_id  ORDER  BY b.bag_id";
        $result = $this->conn->query($sql);
        $data = $this->getArrayDataFromResult($result);

        return ReturnHelper::success($data, "数据请求成功");
    }
}